﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HYSoft.Web
{
    public partial class mapmode1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["uid"] == null || Session["uid"] ==  "")
            {
                Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
                Context.Response.Write("alert('对不起,你登录时间超时,请重新登录');top.location.href=\"index.html\";");
                Context.Response.Write("</script>");
                Context.Response.End();
            }
            if (Request.QueryString["tomonitor"] == null)
                monitornull();
            else
            {
                if (!IsPostBack)
                {
                    Int32 uid = (Int32)Session["uid"];
                    string str = " user_index = " + uid + "";
                    DataSet ds = new BLL.usr2monitor().GetList(str);
                    string s = Request.QueryString["tomonitor"].ToString();
                    DropDownList2.DataSource = ds.Tables[0].DefaultView;
                    DropDownList2.DataTextField = "addr";
                    DropDownList2.DataValueField = "index";
                    DropDownList2.DataBind();
                    DropDownList2.SelectedValue = s;
                    ds.Clear();
                }
                monitor_change();
            }
        }
        protected void monitornull()
        {
            Int32 uid = (Int32)Session["uid"];
            string str = " user_index = " + uid + "";
            DataSet ds = new BLL.usr2monitor().GetList(str);
            string s = ds.Tables[0].Rows[0]["index"].ToString();
            Response.Write("<script>window.location = 'mapmode.aspx?tomonitor=" + ds.Tables[0].Rows[0]["index"].ToString() + "';</script>");
            //string tomo = ds.Tables[0].Rows[0]["index"].ToString();

        }
        protected string map_data()
        {
            string mapdata = "";
            int id = (Int32)Session["uid"];
            string sql = " user_index=" + id + "";
            DataSet ds = new BLL.monitor_info().GetList(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                string Status = "";
                int flag_area = 0; //0正常，1预警，2报警
                int i = 0;
                if (ds.Tables[0].Rows.Count > 1)
                {
                    for (i = 0; i < ds.Tables[0].Rows.Count - 1; i++)
                    {
                        string str = " areano=" + ds.Tables[0].Rows[i]["index"] + "";
                        DataSet dt2 = new BLL.gprsNode().GetList(str);
                        flag_area = 0;
                        for (int j = 0; j < dt2.Tables[0].Rows.Count; j++)
                        {
                            if (dt2.Tables[0].Rows[j]["chuanganqi"].ToString() == "报警")
                            { flag_area = 2; }
                            else if (dt2.Tables[0].Rows[j]["chuanganqi"].ToString() == "预警")
                            {
                                if (flag_area != 2)
                                    flag_area = 1;
                            }
                        }
                        dt2.Clear();
                        dt2.Dispose();

                        switch (flag_area)
                        {
                            case 0: Status = "green;'><b>" + ds.Tables[0].Rows[i]["addr"] + ":  正常</b>"; mapdata = mapdata + "{ url: \"monitor_list.aspx?id=" + ds.Tables[0].Rows[i]["index"] + "\", title:\" " + Status + "\", content: \"本监测点当前状态：正常\", point: \"" + ds.Tables[0].Rows[i]["coordinates_x"] + "|" + ds.Tables[0].Rows[i]["coordinates_y"] + "\", icon: { w: 21, h: 21, l: 0, t: 46, x: 11, lb: 5} }" + ","; break;
                            case 1: Status = "orange;'><b>" + ds.Tables[0].Rows[i]["addr"] + ":  预警</b>"; mapdata = mapdata + "{ url: \"monitor_list.aspx?id=" + ds.Tables[0].Rows[i]["index"] + "\", title:\" " + Status + "\", content: \"本监测点当前状态：预警\", point: \"" + ds.Tables[0].Rows[i]["coordinates_x"] + "|" + ds.Tables[0].Rows[i]["coordinates_y"] + "\", icon: { w: 21, h: 21, l: 66, t: 46, x: 13, lb: 5} }" + ","; break;
                            case 2: Status = "red;'><b>" + ds.Tables[0].Rows[i]["addr"] + ":  报警</b>"; mapdata = mapdata + "{ url: \"monitor_list.aspx?id=" + ds.Tables[0].Rows[i]["index"] + "\", title:\" " + Status + "\", content: \"本监测点当前状态：报警\", point: \"" + ds.Tables[0].Rows[i]["coordinates_x"] + "|" + ds.Tables[0].Rows[i]["coordinates_y"] + "\", icon: { w: 21, h: 21, l: 44, t: 46, x: 12, lb: 5} }" + ","; break;
                            default: break;
                        }

                    }
                }
                string str3 = " areano=" + ds.Tables[0].Rows[i]["index"] + "";
                DataSet dt3 = new BLL.gprsNode().GetList(str3);
                flag_area = 0;
                for (int j = 0; j < dt3.Tables[0].Rows.Count; j++)
                {
                    if (dt3.Tables[0].Rows[j]["chuanganqi"].ToString() == "报警")
                    { flag_area = 2; }
                    else if (dt3.Tables[0].Rows[j]["chuanganqi"].ToString() == "预警")
                    {
                        if (flag_area != 2)
                            flag_area = 1;
                    }
                }
                dt3.Clear();
                dt3.Dispose();

                switch (flag_area)
                {
                    case 0: Status = "green;'><b>" + ds.Tables[0].Rows[i]["addr"] + ":  正常</b>"; mapdata = mapdata + "{ url: \"monitor_list.aspx?id=" + ds.Tables[0].Rows[i]["index"] + "\", title:\" " + Status + "\", content: \"本监测点当前状态：正常\", point: \"" + ds.Tables[0].Rows[i]["coordinates_x"] + "|" + ds.Tables[0].Rows[i]["coordinates_y"] + "\", icon: { w: 21, h: 21, l: 0, t: 46, x: 11, lb: 5} }" + ","; break;
                    case 1: Status = "orange;'><b>" + ds.Tables[0].Rows[i]["addr"] + ":  预警</b>"; mapdata = mapdata + "{ url: \"monitor_list.aspx?id=" + ds.Tables[0].Rows[i]["index"] + "\", title:\" " + Status + "\", content: \"本监测点当前状态：预警\", point: \"" + ds.Tables[0].Rows[i]["coordinates_x"] + "|" + ds.Tables[0].Rows[i]["coordinates_y"] + "\", icon: { w: 21, h: 21, l: 66, t: 46, x: 13, lb: 5} }" + ","; break;
                    case 2: Status = "red;'><b>" + ds.Tables[0].Rows[i]["addr"] + ":  报警</b>"; mapdata = mapdata + "{ url: \"monitor_list.aspx?id=" + ds.Tables[0].Rows[i]["index"] + "\", title:\" " + Status + "\", content: \"本监测点当前状态：报警\", point: \"" + ds.Tables[0].Rows[i]["coordinates_x"] + "|" + ds.Tables[0].Rows[i]["coordinates_y"] + "\", icon: { w: 21, h: 21, l: 44, t: 46, x: 12, lb: 5} }" + ","; break;
                    default: break;
                }
            }
            ds.Clear();
            ds.Dispose();
            return mapdata;
        }
        protected void monitor_change()
        {
            string monitor = DropDownList2.SelectedValue.ToString();
            if (monitor != Request.QueryString["tomonitor"].ToString())
                Response.Write("<script>window.location = 'mapmode.aspx?tomonitor=" + monitor + "';</script>");
        }
        protected string map_center()
        {

            string mapcenter = "";
            if (Request.QueryString["tomonitor"] == null)
                monitornull();
            else
            {
                string s1 = Request.QueryString["tomonitor"].ToString();
                int mon = int.Parse(Request.QueryString["tomonitor"].ToString());
                string s = " [index]=" + mon + "";
                DataSet ds = new BLL.monitor_info().GetList(s);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    mapcenter = ds.Tables[0].Rows[0]["coordinates_x"].ToString() + "," + ds.Tables[0].Rows[0]["coordinates_y"].ToString();
                    ds.Clear();
                    ds.Dispose();
                }
            }
            return mapcenter;
        }

        //protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    UpdatePanel1.Update();
        //    txt_test.Text = this.DropDownList2.Text;
        //}
    }
}